<?php 


 /**
  * This will be the obeject used to store a connection for use in other functions.
  */
  

class Datasource {

        var $dbLink;

       /**
        * Constructor. Call this once when initializing system core.
        * Then save the instance of this class in $connection variable 
        * and pass it as an argument when using services from core.
        */
       function Datasource($dbHost, $dbName, $dbuser, $dbpasswd) {

                // Change this line to reflect whatever Database system you are using:
                $this->dbLink = mysql_connect ($dbHost, $dbuser, $dbpasswd);

                // Change this line to reflect whatever Database system you are using:
                mysql_select_db ($dbName, $this->dbLink);
	}


        /**
         * Function to execute SQL-commands. Use this thin wrapper to avoid 
         * MySQL dependency in application code.
         */
        function execute($sql) {

                // Change this line to reflect whatever Database system you are using:
                $result = mysql_query($sql, $this->dbLink);
                $this->checkErrors($sql);

                return $result;
        }


        /**
         * Function to "blindly" execute SQL-commands. This will not put up 
         * any notifications if SQL fails, so make sure this is not used for 
         * normal operations.
         */
        function executeBlind($sql) {

                // Change this line to reflect whatever Database system you are using:
                $result = mysql_query($sql, $this->dbLink);

                return $result;
        }


        /**
         * Function to iterate trough the resultset. Use this thin wrapper to 
         * avoid MySQL dependency in application code.
         */
        function nextRow ($result) {

                // Change this line to reflect whatever Database system you are using:
                $row = mysql_fetch_array($result);

                return $row;
        }


        /**
         * Check if sql-queries triggered errors. This will be called after an 
         * execute-command. Function requires attempted SQL string as parameter 
         * since it can be logged to application spesific log if errors occurred.
         * This whole method depends heavily from selected Database-system. Make
         * sure you change this method when using some other than MySQL database.
         */
        function checkErrors($sql) {

                //global $systemLog;
                // Only thing that we need todo is define some variables
                // And ask from RDBMS, if there was some sort of errors.
                $err=mysql_error();
                $errno=mysql_errno();

                if($errno) {
                        // SQL Error occurred. This is FATAL error. Error message and 
                        // SQL command will be logged and aplication will teminate immediately.
                        $message = "The following SQL command ".$sql." caused Database error: ".$err.".";

                        //$message = addslashes("SQL-command: ".$sql." error-message: ".$message);
                        //$systemLog->writeSystemSqlError ("SQL Error occurred", $errno, $message);


                        //Calvin and David:
                        //This class simply ends the transaction here,
                        //this is not what we are shooting for. We can simply
                        //create Unique Composite Keys on our database (let the database handle
                        //the complex logic!) and if a duplicate value or something else goes
                        //awry, we throw an exception and let the user continue with the
                        //program!!!!
                        //-Nick
                        print "<font style='color:red'>An error has occurred. All data will be logged.";
                        print "If the error persists contact System Administrator for help! <br/>";
                        print "<!-- ".$message." --></font><br/>";
                        //instead of ending the program
                        //lets handle this tactfully and throw an
                        //exception! :)
                        throw new Exception();

                } else {
                        // Since there was no error, we can safely return to main program.
                        return;
                }
        }
}

?>
